home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbuts.zip / HALFTGA.C < prev    next >
C/C++ Source or Header  |  1991-05-16  |  3KB  |  133 lines

  1. /*********************************************************
  2.  
  3. HALFTGA.C  -  Shrinks a TARGA file to exactly 1/2 its size
  4.           and modifies the header appropriately.
  5.           By Aaron A. Collins, written on 3/30/90
  6.  
  7.           Note - Does NOT interpolate color values!!
  8.              This is to complement PICLAB's inter-
  9.              polating "RESIZE" command...
  10.  
  11.           This file is released to the Public Domain.
  12.  
  13.           PICLAB is a trademark of The Stone Soup Group.
  14.           TARGA is a trademark of AT&T/Truevision.
  15.  
  16. Revised 5/15/91 - AAC - Version 1.1 - Removed IBM-ness of
  17.           filenames/exts.
  18. **********************************************************/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #define MAXXRES 2048   /* huge max x resolution allowable, infinite y res. */
  25.  
  26. unsigned char linbuf[MAXXRES * 3];
  27.  
  28. void main(argc,argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.     int xres, yres, xhi, yhi;
  33.     register int x, y;
  34.     FILE *in, *out;
  35.  
  36.     printf("\n\nTARGA-24 Image File 1/2 Size Resolution Reducer\n");
  37.     printf("Version 1.1 By Aaron A. Collins.  Written 3/30/90 Revised 5/15/91\n\n");
  38.  
  39.     if (argc != 3)
  40.     {
  41.         printf("Usage: %s InputFile OutputFile\n\n",argv[0]);
  42.         exit(1);
  43.     }
  44.  
  45.     if ((in = fopen(argv[1], "rb")) == NULL)  /* try w/supplied ext. */
  46.     {
  47.         printf("ERROR - Couldn't open file %s\n", argv[1]);
  48.         exit(1);
  49.     }
  50.  
  51.     if ((out = fopen(argv[2], "wb")) == NULL)
  52.     {
  53.         printf("ERROR - Couldn't create file %s\n", argv[2]);
  54.         fclose(in);
  55.         exit(1);
  56.     }
  57.  
  58.     /** Copy 1st part of standard Targa header **/
  59.  
  60.     for (x = 0; x < 12; x++)    /* 00, 00, 02, then 9 00's... */
  61.         fputc(fgetc(in), out);
  62.  
  63.     /** load x and y resolution, and rewrite as rest of Targa hdr **/
  64.  
  65.     xres = fgetc(in);            /* lo order */
  66.     xhi = fgetc(in);            /* hi order */
  67.     xres += ((unsigned int) xhi) << 8;
  68.     
  69.     if (xres > MAXXRES)            /* too big? */
  70.     {
  71.         printf("ERROR - X res. of %s (%d) exceeds maximum (%d)!\n", argv[1], xres, MAXXRES);
  72.         fclose(in);            /* close files */
  73.         fclose(out);
  74.         unlink(argv[2]);        /* delete empty out file */
  75.         exit(1);
  76.     }
  77.  
  78.     xhi = xres;                /* save old resolution */
  79.     xres >>= 1;                /* cheapie divide by two! */
  80.  
  81.     fputc(xres & 0x00ff, out);        /* write lo order */
  82.     fputc((xres & 0xff00) >> 8, out);    /* write lo order */
  83.  
  84.     yres = fgetc(in);            /* now do yres the same... */
  85.     yhi = fgetc(in);
  86.     yres += ((unsigned int) yhi) << 8;
  87.  
  88.     yhi = yres;
  89.     yres >>= 1;
  90.  
  91.     fputc(yres & 0x00ff, out);
  92.     fputc((yres & 0xff00) >> 8, out);
  93.  
  94.     fputc(fgetc(in), out);    /* 24 bits/pixel (16 million colors!) (24) */
  95.     fputc(fgetc(in), out);    /* Set bit to indicate top-down display (32)*/
  96.  
  97.     printf("\nInput file        = %s\n", argv[1]);    /* show stats */
  98.     printf("Output file       = %s\n\n", argv[2]);
  99.     printf("Old X  resolution = %d\n", xhi);
  100.     printf("Old Y  resolution = %d\n\n", yhi);
  101.     printf("New X  resolution = %d\n", xres);
  102.     printf("New Y  resolution = %d\n\n", yres);
  103.  
  104.     printf("Processing Line:   0");
  105.  
  106.     for (y = 0; y < yhi; y++)    /* for every line in the old file */
  107.     {
  108.  
  109.         printf("\b\b\b%3d", y);        /* disp. current line # */
  110.  
  111.         fread(linbuf, 3, xhi, in);    /* read in a whole line */
  112.  
  113.         if (feof(in))            /* stop if file truncated */
  114.             break;
  115.  
  116.         if (y & 0x0001)            /* throw away odd # lines */
  117.             continue;
  118.  
  119.         for (x = 0; x < xhi; x++)    /* for every BGR triplet */
  120.         {
  121.             if (x & 0x0001)        /* throw away odd # BGR's */
  122.                 continue;
  123.  
  124.             fwrite(&linbuf[x * 3], 3, 1, out); /*write BGR trip*/
  125.         }
  126.     }
  127.     printf("\n");
  128.     fclose(in);                                 /* close files */
  129.     fflush(out);
  130.     fclose(out);
  131.     exit(0);
  132. }
  133.